package com.shadow.study.java.base.util;

import cn.hutool.core.date.DatePattern;
import com.shadow.study.common.constants.SysConstants;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
 * 时间工具类.
 *
 * @author shadow
 * @since 2023
 * */
public final class DateUtils {

    private static final Long ONE_THOUSAND = 1000L;
    private static final int EIGHT_HOURS = 8;

    public static final DateTimeFormatter DATETIME_YMDHMS = DateTimeFormatter.ofPattern(DatePattern.PURE_DATETIME_PATTERN);

    private DateUtils() {
    }

    /**
     * 时间戳转日期/时间.
     * @param seconds 时间戳
     * @param patternStr 时间格式
     * @return 格式化的时间
     */
    public static String timeStampToDate(final long seconds, final String patternStr) {
        String time = "";
        String  pattern = patternStr;
        if (StringUtils.isBlank(pattern)) {
            pattern = DatePattern.NORM_DATE_PATTERN;
        }
        LocalDateTime dateTime = LocalDateTime.ofEpochSecond(seconds / ONE_THOUSAND, 0, ZoneOffset.ofHours(EIGHT_HOURS));
        if (seconds != 0) {
            time = dateTime.format(DateTimeFormatter.ofPattern(pattern));
        }
        return time;
    }

    /**
     * LocalDateTime转String.
     * @param dateTime 时间
     * @param pattern 时间格式
     * @return String
     */
    public static String dateToString(final LocalDateTime dateTime, final String pattern) {
        String dateStr = "";
        if (null != dateTime) {
            if (StringUtils.isBlank(pattern)) {
                dateStr = dateTime.format(DatePattern.NORM_DATETIME_FORMATTER);
            } else {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
                dateStr = dateTime.format(formatter);
            }
        }
        return dateStr;
    }

    /**
     * LocalDate转String.
     * @param date 时间
     * @param pattern 时间格式
     * @return String
     */
    public static String dateToString(final LocalDate date, final String pattern) {
        String dateStr = "";
        if (null != date) {
            if (StringUtils.isBlank(pattern)) {
                dateStr = date.format(DatePattern.NORM_DATE_FORMATTER);
            } else {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
                dateStr = date.format(formatter);
            }
        }
        return dateStr;
    }

    /**
     * 日期/时间转LocalDateTime.
     * @param date 时间
     * @param patternStr 时间格式
     * @return  时间戳
     */
    public static LocalDateTime dateToDateTime(final String date, final String patternStr) {
        String pattern = patternStr;
        if (StringUtils.isBlank(pattern)) {
            pattern = DatePattern.NORM_DATETIME_PATTERN;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(date, formatter);
    }

    /**
     * 格式化指定格式的.
     * @param pattern 时间格式串
     * @return java.time.LocalDateTime
     **/
    public static LocalDateTime formatToLocalDateTime(final String pattern) {
        LocalDateTime localDateTime = null;
        if (StringUtils.isNotBlank(pattern)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            localDateTime = LocalDateTime.parse(LocalDateTime.now().format(formatter) + SysConstants.PATTERN_HMS_DAWN,
                    DatePattern.NORM_DATETIME_FORMATTER);
        } else {
            localDateTime = LocalDateTime.parse(LocalDateTime.now().format(DatePattern.NORM_DATETIME_FORMATTER),
                    DatePattern.NORM_DATETIME_FORMATTER);
        }
        return localDateTime;
    }
}
